package com.corner.system.service.impl;

import com.alibaba.fastjson2.JSON;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.TeaException;
import com.corner.common.exception.GlobalException;
import com.corner.system.service.AliMessageService;
import org.springframework.stereotype.Service;

/**
 * @program: corner-admin
 * @description: 阿里云短信服务实现类
 * @author: lydms
 * @create: 2023-11-29 13:10
 **/
@Service
public class AliMessageServiceImpl implements AliMessageService {


    @Override
    public void sendMessage(String signName,String templateCode,String phoneNumbers,String templateParam) throws Exception {

        send(signName,templateCode,phoneNumbers,templateParam);
    }


    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     *
     * ulQ6gLfutWJ7o1juqI5k4i8rc3Quyq
     */
    private com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    private void send(String signName,String templateCode,String phoneNumbers,String templateParam) throws Exception{
        // 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
        // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用，仅供参考，建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html
        com.aliyun.dysmsapi20170525.Client client = createClient(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        //com.aliyun.dysmsapi20170525.Client client = AliUtils.createClient("LTAI5tJzrQcgz6LVdMPQYJ44", "ulQ6gLfutWJ7o1juqI5k4i8rc3Quyq");
        com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                .setSignName(signName)//"草根儿体育"
                .setTemplateCode(templateCode)//"SMS_464095737"
                .setPhoneNumbers(phoneNumbers)//"13163679357"
                .setTemplateParam(templateParam);//"{"code":"5758"}"
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();

        // 复制代码运行请自行打印 API 的返回值
        SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
        String s = JSON.toJSONString(sendSmsResponse);
        System.out.println(s);
        System.out.println("发送短信返回：statusCode:"+sendSmsResponse.statusCode+"====> message:"+sendSmsResponse.getBody().getMessage());
        if(!"OK".equals(sendSmsResponse.getBody().getCode())){
            throw new GlobalException("短信发送失败\n原因:"+sendSmsResponse.getBody().getMessage());
        }
        /*try {}
        catch (TeaException error) {
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }*/
    }
}
